package com.xuan.juc.lock;

import java.util.concurrent.atomic.AtomicReference;

public class SpinLockTest {

    // int 0
    // Thread null
    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void myLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"lock");
        // 自旋锁
        while (atomicReference.compareAndSet(null,thread)){

        }
    }
    public void myUnLock(){
        Thread thread = Thread.currentThread();

        atomicReference.compareAndSet(thread,null);
        System.out.println(Thread.currentThread().getName()+"unlock");
    }

}
